package com.itlong.cloud.controller.visitors;

import com.itlong.cloud.POJO.DTO.property.PropertyVisitorPassRecordGetDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyVisitorPassRecordGetVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.property.IPropertyVisitorPassRecordServiceClient;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 * 物业访客管理Controller
 * </desc>
 *
 * @createDate 2018/06/27
 */
@RestController
@RequestMapping("/visitor")
@RefreshScope
public class PropertyVisitorController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyVisitorController.class);

    @Autowired
    IPropertyVisitorPassRecordServiceClient iPropertyVisitorPassRecordServiceClient;

    /**
     * <desc>
     * 访客通行记录列表
     * </desc>
     *
     * @param visitorPassRecordGetDTO 分页查询访客通行记录
     * @return
     * @author caoshuai
     * @createDate 2021/1/22 16:21
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/passRecord/getByPage")
    public Object getVisitorPassRecordByPage(PropertyVisitorPassRecordGetDTO visitorPassRecordGetDTO) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            visitorPassRecordGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyVisitorPassRecordGetDTO.class, request);
            if (visitorPassRecordGetDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            Page<PropertyVisitorPassRecordGetVO> result = iPropertyVisitorPassRecordServiceClient.getVisitorPassRecordByPage(visitorPassRecordGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), result);
        } catch (Exception e) {
            throw new DataAccessException("【运营_访客通行记录】分页查询失败", e);
        }
    }

}